import { push } from 'connected-react-router';
import { ADD_CART_ITEM, REMOVE_CART_ITEM, CLEAR_CART_ITEMS, CHANGE_CART_ITEM_COUNT, CHANGE_CHECKED_CART_ITEMS, SETTLE} from '../action-types';
import { StoreDispatch } from '../index';
import { Lesson } from "@/typings/lesson";
import { message } from "antd";

export default {
    addCartItem(lesson: Lesson) {
        return function (dispatch: StoreDispatch) {
            dispatch({
                type: ADD_CART_ITEM,
                payload: lesson
            });
            message.info("add lesson cuccess!");
        };
    },
    removeCartItem(id: string) {
        return {
            type: REMOVE_CART_ITEM,
            payload: id
        };
    },
    clearCartItems() {
        return {
            type: CLEAR_CART_ITEMS
        };
    },
    changeCartItemCount(id: string, count: number) {
        return {
            type: CHANGE_CART_ITEM_COUNT,
            payload: {
                id,
                count
            }
        };
    },
    changeCheckedCartItems(checkedIds:string[]) {
        return {
            type: CHANGE_CHECKED_CART_ITEMS,
            payload: checkedIds
        };
    },
    settle() {
        return function (dispatch: StoreDispatch) {
            dispatch({
                type: SETTLE
            });
            dispatch(push('/'));
        };
    }
};
